package my.service.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import my.bean.Admin;
import my.dao.IAdminDao;
import my.dao.lmpl.IAdminDaoImpl;
import my.dbUtil.DBUtils;
import my.service.IAdminService;

public class IAdminServiceImpl implements IAdminService {

	@Override
	public int register(Admin admin) throws SQLException {
		Connection conn = null;
		int record = 0;
		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			IAdminDao adminDao = new IAdminDaoImpl(conn);
			record = adminDao.addAdmin(admin);
			return record;
		} catch (SQLException e) {
			System.err.println("注册失败!尝试进行事务回滚!");
			try {
				conn.rollback();
				System.err.println("事务回滚成功!");
			} catch (SQLException e2) {
				System.err.println("事务回滚失败!");
				e2.printStackTrace();
			}
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public int deleteAdminById(int id) throws SQLException {
		Connection conn = null;
		int record = 0;
		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			IAdminDao adminDao = new IAdminDaoImpl(conn);
			record = adminDao.deleteAdminById(id);
			return record;
		} catch (SQLException e) {
			System.err.println("删除失败!尝试进行事务回滚!");
			try {
				conn.rollback();
				System.err.println("事务回滚成功!");
			} catch (SQLException e2) {
				System.err.println("事务回滚失败!");
				e2.printStackTrace();
			}
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public int deleteAdminByAccount(String account) throws SQLException {
		Connection conn = null;
		int record = 0;
		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			IAdminDao adminDao = new IAdminDaoImpl(conn);
			record = adminDao.deleteAdminByAccount(account);
			return record;
		} catch (SQLException e) {
			System.err.println("删除失败!尝试进行事务回滚!");
			try {
				conn.rollback();
				System.err.println("事务回滚成功!");
			} catch (SQLException e2) {
				System.err.println("事务回滚失败!");
				e2.printStackTrace();
			}
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public int modifyBaseMessage(Admin admin) throws SQLException {
		Connection conn = null;
		int record = 0;
		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			IAdminDao adminDao = new IAdminDaoImpl(conn);
			record = adminDao.modifyBaseMessage(admin);
			return record;
		} catch (SQLException e) {
			System.err.println("修改失败!尝试进行事务回滚!");
			try {
				conn.rollback();
				System.err.println("事务回滚成功!");
			} catch (SQLException e2) {
				System.err.println("事务回滚失败!");
				e2.printStackTrace();
			}
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public int modifyAdminPasswordById(int id, String newPassword) throws SQLException {
		Connection conn = null;
		int record = 0;
		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			IAdminDao adminDao = new IAdminDaoImpl(conn);
			record = adminDao.modifyAdminPasswordById(id, newPassword);
			return record;
		} catch (SQLException e) {
			System.err.println("修改失败!尝试进行事务回滚!");
			try {
				conn.rollback();
				System.err.println("事务回滚成功!");
			} catch (SQLException e2) {
				System.err.println("事务回滚失败!");
				e2.printStackTrace();
			}
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public int modifyAdminPrivilegeById(int id, int privilege) throws SQLException {
		Connection conn = null;
		int record = 0;
		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			IAdminDao adminDao = new IAdminDaoImpl(conn);
			record = adminDao.modifyAdminPrivilegeById(id, privilege);
			return record;
		} catch (SQLException e) {
			System.err.println("修改失败!尝试进行事务回滚!");
			try {
				conn.rollback();
				System.err.println("事务回滚成功!");
			} catch (SQLException e2) {
				System.err.println("事务回滚失败!");
				e2.printStackTrace();
			}
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public List<Admin> findAll() {
		List<Admin> list = null;
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			IAdminDao adminDao = new IAdminDaoImpl(conn);
			list = adminDao.findAll();
			return list;
		} finally {
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public Admin findById(int id) {
		Admin admin = null;
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			IAdminDao adminDao = new IAdminDaoImpl(conn);
			admin = adminDao.findById(id);
			return admin;
		} finally {
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public Admin findByAccout(String account) {
		Admin admin = null;
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			IAdminDao adminDao = new IAdminDaoImpl(conn);
			admin = adminDao.findByAccout(account);
			return admin;
		} finally {
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public Admin login(String account, String password) {
		Admin admin = null;
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			IAdminDao adminDao = new IAdminDaoImpl(conn);
			admin = adminDao.findByAccountAndPassword(account, password);
			return admin;
		} finally {
			DBUtils.close(conn, null, null);
		}
	}
}
